package com.nuoniu.sibanyun.controller.so;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.so.VSaleOrderMain;
import com.nuoniu.sibanyun.service.erp.IErpVoucherDesignService;
import com.nuoniu.sibanyun.service.so.IVSaleOrderDetailService;
import com.nuoniu.sibanyun.service.so.IVSaleOrderMainService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 销售订单主表 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-11-26
 */
@RestController
@RequestMapping("/so/saleOrder")
@Api(value="销售订单管理", tags={"销售订单管理"})
public class VSaleOrderMainController extends BaseController {

    @Autowired
    private IVSaleOrderMainService orderMainService;

    /**
     * 保存销售订单
     * @author 小啊刚
     * @date 2020-11-26
     */
    @PostMapping("save")
    @ApiOperation("保存销售订单")
    public HttpResult save(@RequestBody VSaleOrderMain saleOrderMain) {
        if(saleOrderMain == null || saleOrderMain.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("销售订单明细不能为空");
        }
        orderMainService.saveOrder(saleOrderMain);
        return HttpResult.ok();
    }

    /**
     * 获取单据列表
     * @author 小啊刚
     * @date 2020-11-27
     */
    @ApiOperation("获取单据编码")
    @GetMapping("pageList")
    public HttpResult pageList(Page page, VSaleOrderMain saleOrderMain) {
        return HttpResult.ok(orderMainService.pageList(page, saleOrderMain));
    }

    /**
     * 获取单据编码
     * @author 小啊刚
     * @date 2020-11-27
     */
    @ApiOperation("获取单据编码")
    @GetMapping("getCode")
    public HttpResult getCode(String dataSource) {
        return HttpResult.ok(orderMainService.getCode(UserThreadLocal.get().getCompanyId(),dataSource));
    }

    @DeleteMapping("delete")
    @ApiOperation("删除单据")
    public HttpResult delete(Long id) {
        orderMainService.deleteById(id);
        return HttpResult.ok();
    }

    /**
     * 提交
     */
    @GetMapping("submit")
    @ApiOperation("单据提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:提交,-1:反提交")
    })
    public HttpResult submit(Long id, Integer status) {
        orderMainService.submit(id, status);
        return HttpResult.ok();
    }

    /**
     * 审核/弃审
     */
    @GetMapping("examine")
    @ApiOperation("单据审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
        orderMainService.examine(id, status);
        return HttpResult.ok();
    }

    /**
     * 关闭/打开
     */
    @GetMapping("closed")
    @ApiOperation("单据审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 10:关闭,1:打开")
    })
    public HttpResult closed(Long id, Integer status) {
        orderMainService.closed(id, status);
        return HttpResult.ok();
    }


}
